package com.hj.dapp.business.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.category.mapper.CategoryMapper;
import com.hj.dapp.business.category.model.Category;
import com.hj.dapp.business.category.service.CategoryService;
import com.hj.dapp.common.request.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/27
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public IPage<Category> page(PageRequest<Category> pageRequest) {
        Page<Category> page = new Page<>();
        page.setCurrent(pageRequest.getCurrent());
        page.setSize(pageRequest.getSize());
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pageRequest.getQuery())) {
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getName())) {
                wrapper.like("name", pageRequest.getQuery().getName());
            }
        }
        return categoryMapper.selectPage(page,wrapper);
    }

    @Override
    public Category get(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public void save(Category category) {
        categoryMapper.insert(category);

    }

    @Override
    public void edit(Category category) {
        categoryMapper.updateById(category);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public List<Category> list() {
        return categoryMapper.selectList(null);
    }
}
